<template>
    <page-wrapper>
        <table-search @search="search(1)" @reset="reset()">
            <template slot="form">
                <form-item label="">
                    <a-input v-model.trim="form.templateName" placeholder="模板名称" />
                </form-item>
                <form-item label="">
                    <a-input v-model.trim="form.templateCode" placeholder="模板编码" />
                </form-item>
                <!-- <form-item label="">
                    <a-select v-model="form.notifyType" style="width:160px;" allowClear placeholder="请选择通知方式">
                        <a-select-option :value="level.value" v-for="(level,index) in noticeTypes" :key="index">{{level.label}}</a-select-option>
                    </a-select>
                </form-item> -->
            </template>
            <template slot="extra">
                <a-button class="filter-item" type="primary" :disabled="hasPerm(['notice:template:add'])" icon="plus" @click="addInfo()">新增模板</a-button>
                <a-button
                    class="filter-item btn_space"
                    type="danger"
                    @click="remove()"
                    :disabled="!selectionBox.length || hasPerm(['notice:template:remove'])"
                    icon="delete"
                    >删除</a-button
                >
            </template>
        </table-search>
        <table-list
            :loading="loading"
            :columns="columns"
            rowKey="templateId"
            :pagination="pagination"
            :selection="selection"
            :data="list"
        >
            <template slot="index" slot-scope="scope">
                <span>{{scope.index+1+(form.page-1)*form.size}}</span>
            </template>
            <template slot="notifyType" slot-scope="{ text }">
                <field-tag
                    :value="text.notifyType"
                    type="value"
                    showType="label"
                    :data="noticeTypes"
                ></field-tag>
            </template>
            <template slot="action" slot-scope="{ text }">
                <a-button size="small" type="primary" class="btn_space" :disabled="hasPerm(['notice:template:update'])" @click="addInfo(text)">编辑</a-button>
                <a-button size="small" type="danger" class="btn_space" :disabled="hasPerm(['notice:template:remove'])" @click="remove(text)">删除</a-button>
            </template>
        </table-list>

        <template v-if="isShowInfo">
            <template-info :info="itemInfo" @cancel="isShowInfo = false" @submit="submit" />
        </template>
        
    </page-wrapper>
</template>
<script>
import table from '@/mixins/table';
import { columns } from './columns';
import {noticeTypes} from './dictionary'
import TemplateInfo from './components/info';
import {remoteList,deleteInfo} from '@/api/notice/template'

export default {
    name: 'TemplateList',
    mixins: [table],
    components: {
        TemplateInfo
    },
    data() {
        let originForm = {
            templateName: null,
            templateCode: null,
        };
        return {
            originForm,
            noticeTypes,
            form: {
                ...originForm
            },
            columns,
            itemInfo: null,
            isShowInfo: false
        };
    },
    async created(){
        this.search(1)
    },
    methods: {
        async search(page) {
            try {
                this.loading = true;
                if(page){
                    this.form.page = page
                }
                this.list = []
                let res = await remoteList(this.form);
                let { list, total } = res.data;
                this.list = list;
                this.total = total;
                this.selectionBox = []
            } finally {
                this.loading = false;
            }
        },
        //删除
        remove(item = '') {
            this.$confirm({
                title: '提示',
                content: '确定要删除模板？',
                okText: '确定',
                cancelText: '取消',
                onOk:async ()=>{
                    try{
                        let ids=[];
                        if (item) {
                            ids = [item.templateId];
                        } else {
                            let selectionBox = this.selectionBox;
                            selectionBox.map(async item => {
                                ids.push(item.templateId);
                            });
                        }
                        await deleteInfo({ids});
                        this.$message.success('删除成功!');
                        this.search(1);
                    }catch(err){

                    }
                },
                onCancel() {}
            });
        },
        //显示添加
        addInfo(item = {}) {
            this.isShowInfo = true;
            this.itemInfo = item;
        },
        submit() {
            this.isShowInfo = false;
            this.itemInfo = {}
            this.search(1);
        },
    }
};
</script>
